//
//  LeftViewController.m
//  SimpleTimeDemo
//
//  Created by lanou3g on 15/11/12.
//  Copyright © 2015年 王丽珍. All rights reserved.
//

#import "LeftViewController.h"
#import "HeadViewController.h"
#import "UIViewController+RESideMenu.h"
#import "RESideMenu.h"
#import "UMSocial.h"
#import "UIImageView+WebCache.h"

@interface LeftViewController ()


@property (nonatomic, strong) NSMutableArray *dataArray;

@property (strong, nonatomic) IBOutlet UILabel *nameLabel;

@property (strong, nonatomic) IBOutlet UIImageView *headImageView;

- (IBAction)headActionButton:(id)sender;

@property (strong, nonatomic) IBOutlet UITableView *leftTableView;

@property (nonatomic, strong) NSMutableArray *imageArray;


@property (strong, nonatomic) IBOutlet UIImageView *backGroundImage;



@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadLeftTableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showselector) name:@"dataBase" object:nil];
    
    _backGroundImage.image = [UIImage imageNamed:@"20141009123830_25vcd.thumb.700_0"];
    
    _nameLabel.text = @"游客";
    
    
    
#pragma mark --- 根据是否是登陆状态来给图片
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([user boolForKey:@"isLogin"]) {
        // 取出存在本地的数据
        NSString *documentStr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        NSString *path = [documentStr stringByAppendingString:@"/UserInformation.txt"];
        NSArray *arr = [NSArray arrayWithContentsOfFile:path];
        _nameLabel.text = arr[0];
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:arr[1]] placeholderImage:nil];
        
    }else{
        
        // 简单数据本地化
        NSString *documentStr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        NSString *str = nil;
        NSString *imageStr = nil;
        NSArray *array = [NSArray arrayWithObjects:str,imageStr, nil];
        NSString *path = [documentStr stringByAppendingString:@"/UserInformation.txt"];
        [array writeToFile:path atomically:YES];
        NSArray *arr = [NSArray arrayWithContentsOfFile:path];
        NSLog(@"%@",arr);
        NSLog(@"%@",documentStr);
        
        
    }

}

#pragma mark --- 通知中心
- (void)showselector{
    
    _nameLabel.text = @"游客";
    _headImageView.image = [UIImage imageNamed:@"imageView1"];
    
}



#pragma mark--- tableView部分
- (void)loadLeftTableView{
    
    _dataArray = [[NSMutableArray alloc] initWithObjects:@"首页",@"电台",@"阅读",@"社区",@"碎片",@"良品",@"设置", nil];
    
    _imageArray = [[NSMutableArray alloc] initWithObjects:@"iconfont-shouyeshouye",@"iconfont-diantai",@"iconfont-yuedu",@"iconfont-shequ",@"iconfont-iconfonticonfontfangwu",@"iconfont-book",@"iconfont-shezhi", nil];
    
    // 取消cell分割线方法
    _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _leftTableView.backgroundColor = [UIColor clearColor];
    
    // 头像设置圆角
    _headImageView.layer.cornerRadius = 35;
    _headImageView.layer.masksToBounds = YES;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
        cell.textLabel.text = _dataArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:_imageArray[indexPath.row]];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

#pragma mark--- cell的点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"mainVC"]]
                                                         animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 1:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"radioVC"]] animated:YES];
            
            [self.sideMenuViewController hideMenuViewController];
            
            break;
        case 2:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"readVC"]]
                                                         animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
            
        case 3:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"communityVC"]]animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 4:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"fragmentVC"]]animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
            
        case 5:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"produVC"]]
                                                         animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 6:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"setVC"]]
                                                         animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
            
        default:
            break;
    }
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark--- 头像点击事件(登陆)
- (IBAction)headActionButton:(id)sender {
    
    // 用NSUserDefaults 来判断当前是不是登陆状态
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    if (![user boolForKey:@"isLogin"]) {
        
        
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            // 获取微博用户名、uid、token等
            if (response.responseCode == UMSResponseCodeSuccess) {
                
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
                
                NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
                
                
                // 登陆成功之后改变状态
                [user setBool:YES forKey:@"isLogin"];
                
                // 给label上改名字
                _nameLabel.text = snsAccount.userName;
                // 把头像放上去
                [_headImageView sd_setImageWithURL:[NSURL URLWithString:snsAccount.iconURL] placeholderImage:nil];
                
                UIStoryboard *head = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                HeadViewController *headVC = [head instantiateViewControllerWithIdentifier:@"headvcVC"];
                
                [self presentViewController:headVC animated:YES completion:nil];
                
                // 简单数据本地化
                NSString *documentStr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
                
                NSString *str = snsAccount.userName;
                NSString *imageStr = snsAccount.iconURL;
                NSArray *array = [NSArray arrayWithObjects:str,imageStr, nil];
                NSString *path = [documentStr stringByAppendingString:@"/UserInformation.txt"];
                [array writeToFile:path atomically:YES];
                NSArray *arr = [NSArray arrayWithContentsOfFile:path];
                NSLog(@"%@",arr);
                NSLog(@"%@",documentStr);
                
                
            }});

    }else{
    
        UIStoryboard *head = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        HeadViewController *headVC = [head instantiateViewControllerWithIdentifier:@"headvcVC"];
        [self presentViewController:headVC animated:YES completion:nil];
    }
}







@end
